package logic

import (
	"context"
	"errors"
	"furni-manager/model/mysql"

	"furni-manager/furnirpc/furnirpc"
	"furni-manager/furnirpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type FurniDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFurniDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FurniDetailLogic {
	return &FurniDetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *FurniDetailLogic) FurniDetail(in *furnirpc.FFurniDetailRequest) (*furnirpc.FurniDetaiResponse, error) {
	// todo: add your logic here and delete this line

	var furnis mysql.Furni
	mysql.DB.Debug().Where("id = ?", in.FurniId).Find(&furnis)
	if furnis.ID == 0 {
		return nil, errors.New("该家具不存在，请更换")
	}

	err := mysql.DB.Debug().Where("id = ?", in.FurniId).Find(&mysql.Furni{}).Error
	if err != nil {
		return nil, err
	}

	return &furnirpc.FurniDetaiResponse{
		FurniId: int64(furnis.ID),
		Name:    furnis.Name,
		Price:   float32(furnis.Price),
		Stock:   int64(furnis.Stock),
	}, nil
}
